﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDX.Services
{
    /// <summary>
    /// An entry for a service in the <see cref="ServiceProvider"/>.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceEntry<T> where T : class
    {
        List<Func<T>> factories = new List<Func<T>>();

        /// <summary>
        /// Adds the factory.
        /// </summary>
        /// <param name="factory">The factory.</param>
        public void AddFactory(Func<T> factory)
        {
            factories.Add(factory);
        }

        /// <summary>
        /// Resolves an instance.
        /// </summary>
        /// <returns></returns>
        public T Resolve()
        {
            if (factories.Count > 0)
            {
                return factories.First().Invoke();
            }
            return null;
        }

        /// <summary>
        /// Resolves all instances
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> ResolveAll()
        {
            foreach (var factory in factories)
            {
                yield return factory.Invoke();
            }
        }
    }
}
